/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wwcontrolserver.listeners;

import wwcontrolserver.datarecorders.WWDatabaseConnection;
import com.phidgets.InterfaceKitPhidget;
import com.phidgets.Phidget;
import com.phidgets.PhidgetException;
import com.phidgets.event.SensorChangeListener;
import com.phidgets.event.SensorChangeEvent;
import org.tmatesoft.sqljet.core.SqlJetException;
//import wwcontrolserver.ControlFunctionsServer;
//import wwcontrolserver.SensorComputationsServer;
import wwcontrolserver.WWControlServer;
/**
 *
 * @author Michael Ferry
 */
public class WWSensorListener implements SensorChangeListener{
    private WWControlServer WWCS;
    private WWDatabaseConnection WWDB;
    
    WWSensorListener(WWControlServer WWCSin, WWDatabaseConnection WWDBin) {
        this.WWCS = WWCSin;
        this.WWDB = WWDBin;
    }
    
    @Override
    public void sensorChanged(SensorChangeEvent se) {
//        try {
//            Phidget PhidSource = se.getSource();
//            int SerialNumber = PhidSource.getSerialNumber();
//            int SensorIndex = se.getIndex();
//            InterfaceKitPhidget ifk = (InterfaceKitPhidget) PhidSource;
//            //int iState 
//
//            if (SerialNumber == WWControlServer.ifk_HeaterSensor_serial) {
//                for (int i = 0; i < WWControlServer.HeaterSensorMap.length; i++) {
//                    if (SensorIndex == WWControlServer.HeaterSensorMap[i]) {
//                        //Determine if the heater is currently under control
//                        if (WWCS.HeaterControlState[i]) {
//                            ControlFunctionsServer.HeaterControl(SensorIndex, ifk,
//                                    WWControlServer.HeaterSensorToRelayMap[i], WWCS.ifks[WWControlServer.ifk_HeaterRelay], 
//                                    WWCS.TempSetPoint[i], WWCS.TempSetRange[i]);
//                        }
//                    }
//
//                }
//            } 
//            if (WWCS.InputSensorChangeDB && WWDB != null) {
//                try {
//                    int sval = se.getValue();
//                    double cval = SensorComputationsServer.ComputeSensorValue(SerialNumber, SensorIndex, sval, WWControlServer.TENBIT_DYNR);
//                    boolean Ratiometric = ifk.getRatiometric();
//                    int RatiometricInt = Ratiometric ? 1 : 0;
//                    WWDB.InsertSensorRecord(SerialNumber, SensorIndex, sval, cval, RatiometricInt, ifk.getSensorChangeTrigger(SensorIndex), WWControlServer.TENBIT_DYNR, 1);
//                } catch (SqlJetException sqle) {
//                    System.out.println(sqle);
//                    WWCS.RecordLogEntry(sqle.toString());
//                }
//            }
//            
//            
//        }catch (PhidgetException phde){
//            System.out.println(phde);
//            WWCS.RecordLogEntry(phde.toString());
//        }
    }
    
}
